package com.lucas.servlet.chapter3_3.mapper;

import com.lucas.servlet.chapter3_3.entity.User;
import org.apache.ibatis.annotations.*;

//使用Mapper注解，让UserMapper变成MyBatis的映射器
@Mapper
public interface UserMapper {
    @Results(id = "usermap", value = {  //定义一个@Result注解集合
            @Result(property = "name", column = "username"),   //将数据库中username一列值映射到name上，可以处理变量名与列名不一致的情况
            @Result(property = "password", column = "password"),
            @Result(property = "sex", column = "sex")

    })

    @Select("select *from users where  username= #{username}")
    User getUserByName(@Param("username") String name, String password);  //抽象方法

    //新增，占位符会自动和方法里的参数进行映射，如果参数是实体类，那么要求实体类对象参数名字和占位符对应
    @Insert("insert into users(username, password, sex) values (#{name}, #{password}, #{sex})")
    int createUser(User user);

    @Update("update users set username = #{name}, password = #{password}, sex = #{sex} where id = #{id}")
    int updateUser(User user);

    // 根据用户名删除用户的方法
    @Delete("delete from users where username = #{username}")
    int deleteUserByUsername(@Param("username") String username);

    @Select("select count(1) from users where username=#{name}")
    int findUserByName(User user);
}


